package figure8.GUI;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import figure8.Controller;

/**
 * The control panel is composed of all the buttons to control playback and
 * recording. It forwards the commands to the controller.
 */
public class ControlPanel extends JPanel implements ActionListener {
    public enum ControlPanelStates {
        RECORDING, STOPPED, PROJECTLESS
    }

    private Controller controller;
    private ControlPanelStates state;

    private JButton recordButton;
    private JButton keepButton;
    private JButton stopButton;

    public ControlPanel(Controller controller) {
        super();

        this.controller = controller;
        
        // Use a grid layout of 1 row and 4 columns to give each button equal
        // space.
        setLayout(new GridLayout(1, 4));

        // Create the buttons, add the control panel as a listener to them and
        // add them to the control panel.
        recordButton = new JButton("Record");
        keepButton = new JButton("Keep");
        stopButton = new JButton("Stop");

        recordButton.addActionListener(this);
        keepButton.addActionListener(this);
        stopButton.addActionListener(this);
        
        add(recordButton);
        add(keepButton);
        add(stopButton);
        
        // Set the minimum size of the buttons
        for (Component button : getComponents()) {
            button.setPreferredSize(new Dimension(70, 30));
        }
        
        // Set the state of the control panel as projectless (all buttons are
        // greyed out).
        setState(ControlPanelStates.PROJECTLESS);
    }

    /**
     * Set the state of the panel, which decided which buttons are available to
     * be pressed and which are greyed out.
     */
    public void setState(ControlPanelStates state) {
        this.state = state;

        switch (state) {
        case RECORDING:
            recordButton.setEnabled(false);
            keepButton.setEnabled(true);
            stopButton.setEnabled(true);
            break;
        case STOPPED:
            recordButton.setEnabled(true);
            keepButton.setEnabled(false);
            stopButton.setEnabled(false);
            break;
        case PROJECTLESS:
            for (Component button : getComponents()) {
                button.setEnabled(false);
            }
        }
    }
    
    public void stopped () {
        recordButton.setEnabled(true);
        keepButton.setEnabled(false);
        stopButton.setEnabled(false);
    }
    
    /* 
     * Forward button presses to the controller and set the appropriate state
     * of the control panel.
     */
    @Override
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == recordButton) {
            setState(ControlPanelStates.RECORDING);
            controller.record();
        } else if (event.getSource() == keepButton) {
            controller.keep();
        } else if (event.getSource() == stopButton) {
            setState(ControlPanelStates.STOPPED);
            controller.stop(); 
        } 
    }
    
    public void keyPressed(KeyEvent e)
    {
    	int key = e.getKeyCode();
    	if (key == KeyEvent.VK_SPACE)
    	{
    		controller.keep();
    	}
    }
}
